package com.demo.core.service.base.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.MemberDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Member;
import com.demo.core.service.base.MemberService;
import com.demo.core.service.exception.MemberAlreadyExistedException;
import com.demo.core.service.exception.UserNotFoundException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员服务实现类
 * @author ryuu.kk
 *
 */
public class MemberServiceImpl implements MemberService {
	/**
	 * 日志
	 */
	private static final Log LOG = LogFactory.getLog(MemberServiceImpl.class);
	/**
	 * 会员服务接口
	 */
	private MemberDao memberDao;
	
	@Override
	public void delete(Long memberId) {
		memberDao.delete(memberId);
	}
	
	@Override
	@Transactional
	public Long save(Member member) throws UserNotFoundException, MemberAlreadyExistedException {
		
		Map<String, Object> params = new HashMap<String, Object>();
		Long userId = member.getUserId();
		Long siteId = member.getSiteId();
		params.put("siteId", siteId);
		params.put("userId", userId);
		List<Member> memberList = memberDao.select(params);
		if (memberList.size() > 0) {
			//会员以存在
			if (memberList.size() != 1) {
				//出现同站点多个用户存在的会员,数据错误
				LOG.error("T_CORE_MEMBER 数据错误. 在站点ID " + 
						siteId + "中, 条件(userID= "+ userId + ")找到" + 
						memberList.size()+ "条数据");
			}
			MemberAlreadyExistedException e = new MemberAlreadyExistedException(memberList.get(0), "member had already existed.");
			// 增加一个异常的国际化code
			e.setI18n("member.already.existed");
			throw e;
		}
		Long id = memberDao.insert(member);
		if (LOG.isDebugEnabled()) {
			LOG.debug("Create Member [id:" + id + ", site:" + siteId + " , userId:" + userId + " + ]");
		}
		return id;
	}
	
	@Override
	public boolean exists(Long id) {
		return memberDao.exists(id);
	}
	
	@Override
	public void updateNickName(Long id, String nickName) {
		Member member = new Member();
		member.setNickName(nickName);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		memberDao.update(member, params);
	}

	@Override
	public void enable(Long id, boolean enabled) {
		updateStatus(id, null, enabled, null, null);
	}
	
	@Override
	public void lock(Long id, boolean locked) {
		updateStatus(id, null, null, locked, null);
	}
	
	@Override
	public void isValid(Long id, boolean isValid) {
		updateStatus(id, null, null, null, isValid);
	}
	
	@Override
	public void expireDay(Long id, Date expireDay) {
		updateStatus(id, expireDay, null, null, null);
	}
	
	protected void updateStatus(Long id, Date expireDay, Boolean enabled,
			Boolean locked, Boolean isValid) {
		Member member = new Member();
		member.setEnabled(enabled);
		member.setLocked(locked);
		member.setIsValid(isValid);
		member.setExpireDay(expireDay);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		memberDao.updateStatus(member, params);
	}
	
	@Override
	public void updateLastLogin(Long id, Date lastLoginTime, String lastLoginIp) {
		Member member = new Member();
		member.setLastLoginTime(lastLoginTime);
		member.setLastLoginIp(lastLoginIp);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		memberDao.updateLast(member, params);
	}
	
	@Override
	public void updateCurrentLogin(Long id, Date currentLoginTime, String currentLoginIp, int loginCount) {
		Member member = new Member();
		member.setCurrentLoginTime(currentLoginTime);
		member.setCurrentLoginIp(currentLoginIp);
		member.setLoginCount(loginCount);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		memberDao.updateCurrent(member, params);
	}
	
	@Override
	public Member find(Long memberId) {
		return memberDao.select(memberId);
	}

	@Override
	public Member find(String userName, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName);
		params.put("siteId", siteId);
		List<Member> memberList = memberDao.select(params);
		if (memberList.size() == 1) {
			return memberList.get(0);
		}
		return null;
	}
	
	@Override
	public Member find(Long userId, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("siteId", siteId);
		List<Member> memberList = memberDao.select(params);
		if (memberList.size() == 1) {
			return memberList.get(0);
		}
		return null;
	}
	@Override
	public List<Member> findJoin(Long userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		return memberDao.select(params);
	}
	@Override
	public List<Member> findList(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		return memberDao.select(params);
	}
	
	@Override
	public Member query(Long memberId) {
		return memberDao.query(memberId);
	}
	
	@Override
	public Member query(Long memberId, Boolean mEnabled, Boolean rEnabled) {
		return memberDao.query(memberId);
	}
	
	@Override
	public ResultSearchResult<Member> search(Long siteId, SearchArgumentSupport args) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return memberDao.search(params, args);
	}
	
	@Override
	public ResultSearchResult<Member> searchUnion(Long siteId, SearchArgumentSupport args) {
		return memberDao.searchUnion(siteId, args);
	}
	
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
}
